package hust.cs.javacourse.search.index.impl;

import hust.cs.javacourse.search.index.AbstractDocument;
import hust.cs.javacourse.search.index.AbstractTermTuple;

import java.util.Iterator;
import java.util.List;

public class Document extends AbstractDocument {
    /**
     * 缺省构造函数
     */
    public Document() {
        super();
    }

    /**
     * 带参构造函数
     * @param docId
     * @param docPath
     */
    public Document (int docId, String docPath) {
        super(docId, docPath);
    }

    public Document (int docId, String docPath, List<AbstractTermTuple> tuples) {
        super(docId, docPath, tuples);
    }

    /**
     * 返回文档ID
     * @return
     */
    @Override
    public int getDocId() {
        return this.docId;
    }

    /**
     * 设置文档ID
     * @param docId：文档id
     */
    @Override
    public void setDocId(int docId) {
        this.docId = docId;
    }

    /**
     * 返回文档路径
     * @return
     */
    @Override
    public String getDocPath() {
        return this.docPath;
    }

    /**
     * 设置文档绝对路径
     * @param docPath：文档绝对路径
     */
    @Override
    public void setDocPath(String docPath) {
        this.docPath = docPath;
    }

    /**
     * 返回三元组列表
     * @return
     */
    @Override
    public List<AbstractTermTuple> getTuples() {
        return this.tuples;
    }

    /**
     * 添加一个三元组
     * @param tuple ：要添加的三元组
     */
    @Override
    public void addTuple(AbstractTermTuple tuple) {
        if(this.tuples.contains(tuple))
            return;
        this.tuples.add(tuple);
    }

    /**
     * 判断是否包含指定的三元组
     * @param tuple ： 指定的三元组
     * @return
     */
    @Override
    public boolean contains(AbstractTermTuple tuple) {
        Iterator it = this.tuples.iterator();
        while(it.hasNext()) {
            if(it.next().equals(tuple))
                return true;
        }
        return false;
    }

    /**
     * 获取指定的三元组
     * @param index：指定下标位置
     * @return
     */
    @Override
    public AbstractTermTuple getTuple(int index) {
        return this.tuples.get(index);
    }

    /**
     * 返回三元组的大小
     * @return
     */
    @Override
    public int getTupleSize() {
        return this.tuples.size();
    }

    @Override
    public String toString() {
        String info = "docId: " + this.docId;
        info = info + "     docPath: " + this.docPath;
        Iterator it = this.tuples.iterator();
        while(it.hasNext()) {
            info = info + "\n<" + it.next().toString() + ">";
        }
        return info;
    }
}
